package com.sphd.Restaurant.fragments.homefragments;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.sphd.Restaurant.R;
import com.sphd.Restaurant.activity.RestaurantPageActivity;
import com.sphd.Restaurant.base.LazyLoadFragment;
import com.sphd.Restaurant.infos.RestaurantInfo;
import com.sphd.Restaurant.model.Model;
import com.sphd.Restaurant.presenter.OftenP;
import com.sphd.Restaurant.utils.Constants;
import com.sphd.Restaurant.views.OftenV;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Hu on 2016/9/27.
 */

public class OftenAllrestaurantsFragment extends LazyLoadFragment implements OftenV {

    @BindView(R.id.my_recy)
    RecyclerView myRecy;
    @BindView(R.id.my_srf)
    SwipeRefreshLayout mySrf;
    BaseQuickAdapter<RestaurantInfo> baseQuickAdapter;
    List<RestaurantInfo> infos;

    @Override
    public int getLayout() {
        return R.layout.recy_and_swf;
    }

    @Override
    public void initViews(View view) {
        ButterKnife.bind(this, view);

        initEvent();
        infos = new ArrayList<>();
        setRefresh(mySrf);
        setRecy(myRecy);
        baseQuickAdapter = new BaseQuickAdapter<RestaurantInfo>(R.layout.item_rest, infos) {
            @Override
            protected void convert(BaseViewHolder helper, RestaurantInfo item) {
                ImageView imageView = helper.getView(R.id.iv_restaurant_img);
                setImage(imageView,item.getCg_pic());
                helper.setText(R.id.tv_restaurantname, item.getCg_name())
                        .setText(R.id.tv_openTime,"营业时间:" +item.getCg_openst()+"~" +item.getCg_openet());
                if (item.isCg_isosale()){
                    helper.setText(R.id.tv_DeliverMealsTime,"送餐时间:" + item.getCg_sendst()+"~"+item.getCg_sendet());
                }

                TextView tvIsDeliverMeals = helper.getView(R.id.tv_isDeliverMeals);
                TextView tvIsCoupon = helper.getView(R.id.tv_isCoupon);
                TextView tvIsRecharge = helper.getView(R.id.tv_isRecharge);
                if (item.isCg_isosale()) {
                    tvIsDeliverMeals.setVisibility(View.GONE);
                } else {
                    tvIsDeliverMeals.setVisibility(View.GONE);
                }
                if (item.isCg_isyh()) {
                    tvIsCoupon.setVisibility(View.VISIBLE);
                } else {
                    tvIsCoupon.setVisibility(View.GONE);
                }
                if (item.isCg_iscz()) {
                    tvIsRecharge.setVisibility(View.VISIBLE);
                } else {
                    tvIsRecharge.setVisibility(View.GONE);
                }
            }
        };
        myRecy.setAdapter(baseQuickAdapter);
    }

    private void initEvent() {
        mySrf.setOnRefreshListener(() -> {
            if (getBoolean(Constants.ISLOGIN)) {
                infos.clear();
                loadData();
            }else {
                mySrf.setRefreshing(false);
                toast("请登录后查看刷新。");
            }

        });

         /*点击事件*/
        myRecy.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void SimpleOnItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle =new Bundle();
                bundle.putString(Constants.TAG,context.getClass().getName());
                bundle.putSerializable(Constants.RESTAURANT,infos.get(position));
                startActivity(RestaurantPageActivity.class,bundle);
            }
        });
    }

    @Override
    public void loadData() {
        if (getBoolean(Constants.ISLOGIN)) {
            OftenP oftenP = new OftenP(this);
            oftenP.addData(Model.HTTPURL + Model.OFFTENUSE + "phone=" + getString(Constants.USERPHONE));
        }

    }

    @Override
    public void refreshList(List<RestaurantInfo> getmsg) {
        if (getmsg.size() != 0) {
            infos.addAll(getmsg);
        }
        baseQuickAdapter.notifyDataSetChanged();
        setLoadDataCompleted(true);

    }

    @Override
    public void showdialog() {
        showDialog();
    }

    @Override
    public void dissmissdialog() {
        dissMissDialog();
        mySrf.setRefreshing(false);
    }
}
